package com.github.prontera.ueditor.config;

import java.io.BufferedInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigLoader {
	
	private static final Logger logger = LoggerFactory.getLogger(ConfigLoader.class);
	private static final String DEFAULT_FILENAME = "config.json";
	private static byte[] json;
	
	public static byte[] getConfig() {
		loadConfigUtil();
		return json;
	}
	
	public static void loadConfigUtil() {
		if(json!=null) {
			return;
		}
		InputStream in = ConfigLoader.class.getResourceAsStream(DEFAULT_FILENAME);
		if (in == null) {
			logger.error("{} not found", DEFAULT_FILENAME);
			throw new RuntimeException(DEFAULT_FILENAME + " not found");
		} else {
			if (!(in instanceof BufferedInputStream)) {
				in = new BufferedInputStream(in);
			}
			try {
				json=IOUtils.toByteArray(in);
				System.out.println("------json-------["+new String(json)+"]");
				in.close();
				logger.debug("{} loaded", DEFAULT_FILENAME);
			} catch (Exception e) {
				logger.error("Error while processing {}", DEFAULT_FILENAME);
				throw new RuntimeException("Error while processing " + DEFAULT_FILENAME, e);
			}
		}
	}

 
}
